package railRoad;

import java.util.ArrayList;

/**
 * Class that presents Route data
 * 
 * @author Rukin Oleg
 * 
 */
public class Route {
	private int route_id = -1;
	private ArrayList<Station> stations;
	private String routeName;

	private int availableTicket = 0;

	public Route(int route_id, int availableTicket) {
		this.route_id = route_id;

		if (availableTicket > 0)
			this.availableTicket = availableTicket;

		this.stations = new ArrayList<Station>();
		this.routeName = new String();

		createName();
	}

	public Route(Route route) {
		this.stations = new ArrayList<Station>(route.stations);
		this.routeName = new String(route.routeName);
		this.availableTicket = route.availableTicket;

		createName();
	}

	public int getRoute_id() {
		return route_id;
	}

	public void setRoute_id(int route_id) {
		this.route_id = route_id;
	}

	/**
	 * Name of Route
	 * 
	 * @return name of this Route
	 */
	public String getRouteName() {
		return routeName.toString();
	}

	/**
	 * Get availableTicket parameter
	 * 
	 * @return number of available tickets for
	 */
	public int getAvailableTicket() {
		return availableTicket;
	}

	public void setAvailableTicket(int availableTicket) {
		this.availableTicket = availableTicket;
	}

	/**
	 * Return station from station list by it index
	 * 
	 * @param index
	 *            index in List
	 * @return {@link#Station}
	 */
	public Station getStationAt(int index) {
		return stations.get(index);
	}

	/**
	 * Return station from station list by it id
	 * 
	 * @param id
	 *            station id to search
	 * @return {@link#Station}
	 */
	public Station getStationById(int id) {

		Station station = null;

		int size = stations.size();

		for (int i = 0; i < size; i++) {
			station = stations.get(i);
			if (station.getStation_id() == id) {
				return station; // we found it
			}// if
		}// for

		return null; // we didn't found it
	}

	public int lengthBetweenStations(int first, int second) {
	
		int indexFirst = getIndexByStation(first);
		int indexSecond = getIndexByStation(second);
		
		int rez = 0;
		
		if(indexFirst > -1 && indexSecond > -1)
		{
			rez = Math.abs(indexFirst - indexSecond); 
		}
		
		return rez;
	}

	/**
	 * Method to add {@link#Station} to Route
	 * 
	 * @param station
	 *            {@link#Station} to add
	 */
	public void addStation(Station station) {
		stations.add(station);

		createName();
	}

	/**
	 * Method to remove {@link#Station} from Route
	 * 
	 * @param station
	 *            {@link#Station} to remove
	 */
	public void removeStation(Station station) {
		stations.remove(station);

		createName();
	}

	/**
	 * Method to check is there some available tickets
	 * 
	 * @return true if there are available tickets else return false
	 */
	public boolean checkTicketonAvailaible() {
		return (availableTicket > 0);
	}

	/**
	 * Method to buy one {@link Ticket}
	 * 
	 * @return
	 */
	public boolean buyTicket() {
		boolean rez = checkTicketonAvailaible();

		if (rez) {
			availableTicket--;
		}

		return rez;
	}

	/**
	 * Method to increase available tickets
	 */
	public void returnTicket() {
		availableTicket++;
	}

	/**
	 * Get length of this Route number of {@link Station}
	 * 
	 * @return count of stations in route
	 */
	public int size() {
		return stations.size();
	}// getLength()

	@Override
	public String toString() {
		StringBuilder out = new StringBuilder("ID: " + this.route_id
				+ "\nTickets available: " + this.availableTicket + "\n");

		for (Station station : stations) {
			out.append("\t" + station.toString());
		}

		return out.toString();
	}

	private void createName() {
		if (stations != null && stations.size() > 0) {
			int count = stations.size() > 1 ? stations.size() - 1 : 0;
			this.routeName = stations.get(0).getStationName() + "-"
					+ stations.get(count).getStationName();
		}// if
	}// createName

	private int getIndexByStation(int id) {
		int size = stations.size();
		for (int i = 0; i < size; i++) {
			if (stations.get(i).getStation_id() == id)
				return i;
		}
		return -1;
	}

}
